var progect_type = null;
var pro_big = null;
var progect_type_one = null;
var bbudgetstandard = null;
$(function () {
    $.when(getbbudgetstandard(),    getdatadictionary("progect_type"),getdatadictionary("pro_big"),getdatadictionary("progect_type_one") ).done(
    $("#jqGrid").jqGrid({
        url: baseURL + 'api/bestimate/list?aguid='+GetUrlParam("aguid"),
        datatype: "json",
        colModel: [			
			{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true , hidden: true},
			{ label: '所属项目申请书ID', name: 'aGuid', index: 'a_guid', width: 80 , hidden: true},
			{ label: '所属预算标准', name: 'bGuid', index: 'b_guid', width: 80 ,
                formatter: function (cellvalue, options, rowObject) {
                    var names = "";

                    for (d in bbudgetstandard) {
                        if (cellvalue==bbudgetstandard[d].guid) {
                            names = bbudgetstandard[d].bSName ;
                        }
                    }

                    return names;
                }
			},
			{ label: '项目类别', name: 'eType', index: 'e_type', width: 80 ,
                formatter: function (cellvalue, options, rowObject) {
                    var names = "";

                    for (d in progect_type) {
                        if (cellvalue==progect_type[d].dKey) {
                            names = progect_type[d].dValue ;
                        }
                    }

                    return names;
                }
			},
			{ label: '项目大类', name: 'eDType', index: 'e_d_type', width: 80 ,
                formatter: function (cellvalue, options, rowObject) {
                    var names = "";

                    for (d in pro_big) {
                        if (cellvalue==pro_big[d].dKey) {
                            names = pro_big[d].dValue ;
                        }
                    }

                    return names;
                }
			},
			{ label: '估算项', name: 'eItem', index: 'e_item', width: 80 ,
                formatter: function (cellvalue, options, rowObject) {
                    var names = "";

                    for (d in progect_type_one) {
                        if (cellvalue==progect_type_one[d].dKey) {
                            names = progect_type_one[d].dValue ;
                        }
                    }

                    return names;
                }
			},
			{ label: '系数名称', name: 'eName', index: 'e_name', width: 80 }, 			
			{ label: '综合调整系数', name: 'eSysRatio', index: 'e_sys_ratio', width: 80 }, 			
			{ label: '估算调整系数', name: 'eOwnRatio', index: 'e_own_ratio', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    })
	)
});
function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "progect_type") {
            progect_type = r.returnlist;
        }
        if (sss == "pro_big") {
            pro_big = r.returnlist;
        }
        if (sss == "progect_type_one") {
            progect_type_one = r.returnlist;
        }

        vm.progect_type =progect_type;
    });
}
function getbbudgetstandard() {

    $.get(baseURL + "api/bbudgetstandard/select" , function (r) {
        bbudgetstandard= r.returnlist;
      });
}

var vm = new Vue({
	el:'#rrapp',
	data:{

        q:{
         eType: null
         },
		showList: true,
		title: null,
        aGuid: GetUrlParam("aguid"),
		bEstimate: {},
        progect_type: progect_type,
        pro_big: pro_big,
        progect_type_one: progect_type_one,
        bbudgetstandard:bbudgetstandard
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bEstimate =  {aGuid:GetUrlParam("aguid")};
            vm.progect_type = progect_type;
            vm.pro_big = pro_big;
            vm.progect_type_one = progect_type_one;
            vm.bbudgetstandard = bbudgetstandard;

		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.aGuid = GetUrlParam("aguid");
            vm.progect_type = progect_type;
            vm.pro_big = pro_big;
            vm.progect_type_one = progect_type_one;
            vm.bbudgetstandard = bbudgetstandard;
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.bEstimate.guid == null ? "api/bestimate/save" : "api/bestimate/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bEstimate),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "api/bestimate/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "api/bestimate/info/"+guid, function(r){
                vm.bEstimate = r.bEstimate;
                vm.bEstimate.aGuid = GetUrlParam("aguid");
            });
		},
		reload: function (event) {

			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'eType': vm.q.eType},
                page:page
            }).trigger("reloadGrid");
		}
	}
});